package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
   DishMapper dishMapper;
    @Autowired
   SetmealMapper setmealMapper;

    /*
    * 根据id删除分类
    * */
    @Override
    public R<String> deleteById(Long id) {
        //统计关联菜品计数
        LambdaQueryWrapper<Dish> dishLqw =new LambdaQueryWrapper<>();
        dishLqw.eq(Dish::getCategoryId,id);
        Integer count = dishMapper.selectCount(dishLqw);
        if (count>0){
            //有关联不能删除
            return R.error("当前菜品有关联，不能删除");
        }
        LambdaQueryWrapper<Setmeal> setmealLqw =new LambdaQueryWrapper<>();
        setmealLqw.eq(Setmeal::getCategoryId,id);
        count = setmealMapper.selectCount(setmealLqw);
        if (count>0){
            //有关联不能删除
            return R.error("当前套餐有关联，不能删除");
        }
        //无关联 可以删除
        categoryMapper.deleteById(id);
        return R.success("无关联,可以删除");
    }

}
